<?php
/**
 * GeryEngine
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this release in the file LICENSE
 *
 * @copyright  Copyright (c) 2007 Arjen Brouwer
 * @license    New BSD License
 * @author     Arjen Brouwer
 */

/**
 * General purpose implemention of the application Context 
 * interface for general application use. This class should 
 * be used in place of the original IGeryContext class.
 *
 * This implementation uses a HashMap (@see java.util.HashMap ) 
 * for data storage.
 *
 * This context implementation cannot be shared between threads
 * without those threads synchronizing access between them, as 
 * the HashMap is not synchronized, nor are some of the fundamentals
 * of AbstractContext. If you need to share a Context between 
 * threads with simultaneous access for some reason, please create 
 * your own and extend the interface Context 
 * 
 * @see org.apache.velocity.context.Context
 *
 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
 * @author <a href="mailto:fedor.karpelevitch@home.com">Fedor Karpelevitch</a>
 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
 * @version $Id: VelocityContext.java,v 1.6.8.1 2004/03/03 22:27:36 geirm Exp $
 */
class GeryContext extends AbstractGeryContext
{
	/**
	 * Storage for key/value pairs.
	 * 
	 * @var HashMap $context
	 */
	private $context = null;

	/**
	 * Initializes internal storage (never to <code>null</code>), and
	 * inner context.
	 *
	 * @param array $context Internal storage, or <code>null</code> to
	 * create default storage.
	 */
	public function __construct($context = null)
	{
		// set the current context
		if ($context == null) 
		{
			$this->context = new HashMap();
		} 
		else if ($context instanceof GeryContext) 
		{
			$this->context = $context->getStorageHash();
		} 
		else if ($context instanceof HashMap)
		{
			$this->context = $context;
		}
		else if (is_array($context))
		{
			$this->context = new HashMap($context);
		}
	}

	/**
	 * Retrieves value for key from internal
	 * storage
	 *
	 * @param key name of value to get
	 * @return value as object
	 */
	public function &internalGet($key)
	{
		return $this->context->get($key);
	}		

	/**
	 * stores the value for key to internal
	 * storage
	 *
	 * @param key name of value to store
	 * @param value value to store
	 * @return previous value of key as Object
	 */
	public function internalPut($key, $value)
	{
		return $this->context->put($key, $value);
	}

	/**
	 * determines if there is a value for the
	 * given key
	 *
	 * @param key name of value to check
	 * @return true if non-null value in store
	 */
	public function internalContainsKey($key)
	{
		return $this->context->containsKey($key);
	}

	/**
	 * Returns array of keys
	 *
	 * @return array
	 */
	public function internalGetKeys()
	{
		return $this->context->keys();
	}
	
	/**
	 * remove a key/value pair from the
	 * internal storage
	 *
	 * @param key name of value to remove
	 * @return value removed
	 */
	public function internalRemove($key)
	{
		return $this->context->remove($key);
	}
	
	private function getStorageHash() {
		return $this->context;
	}
}
